package com.hurl.waguji.biz.download;

import java.util.Date;

import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.DateUtils;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.Formatter;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjCwLrbHexunBO;
import com.hurl.waguji.persistence.JcsjCwLrbHexunBOId;

class CwLrbHexunProcessor extends BaseDownloadForUpdateProcessor {


	public CwLrbHexunProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
	}

	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {

		if(StringUtils.isBlank(html)){
			return;
		}

		JcsjCwLrbHexunBO bo  = html2BO(context.getEnvAsString("gpdm"),html);
		if(bo!=null){
			collector.addBO(bo);
		}
	}

	private JcsjCwLrbHexunBO html2BO(String gpdm, String html){
		if(StringUtils.isBlank(html)){
			return null;
		}
		html = StringUtils.substringBetween(html, "<span id=\"ControlEx1_lbl\">", "</span>");
		if(StringUtils.isBlank(html)){
			return null;
		}
		JcsjCwLrbHexunBO bo = new JcsjCwLrbHexunBO();
		
		String tmp = StringUtils.substringBetween(html, "会计年度", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setId(new JcsjCwLrbHexunBOId(gpdm,Formatter.toDate(tmp)));
		
		tmp = StringUtils.substringBetween(html, "、营业收入<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setYysr(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">减：营业成本<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setYycb(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">营业税金及附加<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setYysjjfj(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">销售费用<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setXsfy(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">管理费用<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setGlfy(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">勘探费用<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setKtfy(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">财务费用<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setCwfy(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">资产减值损失<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setZcjzss(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, "公允价值变动净收益<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setGyjzbdjsy(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">投资收益<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setTzsy(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, "对联营企业和合营企业的投资收益<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setDlyqyhhyqydtzsy(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">影响营业利润的其他科目<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setYxyylrdqtkm(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, "、营业利润<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setYylr(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">补贴收入<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setBtsr(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">营业外收入<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setYywsr(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">减：营业外支出<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setYywzc(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">其中：非流动资产处置净损失<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setFldzcczjss(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">加：影响利润总额的其他科目<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setYxlrzedqtkm(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, "、利润总额<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setLrze(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">减：所得税<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setSds(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">加：影响净利润的其他科目<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setYxjlrdqtkm(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, "、净利润<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setJlr(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">归属于母公司所有者的净利润<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setGsmgssyzjlr(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">少数股东损益<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setSsgdsy(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, "、每股收益<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setMgsy(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, "基本每股收益<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setJbmgsy(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, "稀释每股收益<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setXsmgsy(Formatter.toDouble(tmp));
		
		tmp = StringUtils.substringBetween(html, ">备注<", "<tr>");
		tmp = StringUtils.substringBetween(tmp, "class='tishi'>", "</div>");
		bo.setBz(Formatter.toString(tmp));
		
		bo.setLastupdate(currentTime);
		
		return bo;
	}

	@Override
	public UrlPattern getUrlPattern() {
		return new StockIteratorUrlPattern(context) {
			@Override
			public String bo2url(DmGpBO bo) {
				String dateStr = DateUtils.formatDate(getRq(), "yyyy.MM.dd");
				return "http://stockdata.stock.hexun.com/2008/lr.aspx?stockid="+bo.getGpDmJ()+"&accountdate="+dateStr;
			}
		};
	}
}
